package com.example.ssmxuankeback.controller;

import com.example.ssmxuankeback.common.RestBean;
import com.example.ssmxuankeback.dto.CourseDTO;
import com.example.ssmxuankeback.dto.CoursePageQueryDTO;
import com.example.ssmxuankeback.service.CourseService;
import com.example.ssmxuankeback.vo.CourseVO;
import com.example.ssmxuankeback.vo.PageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Validated
@Tag(name = "CourseController", description = "课程内容")
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    CourseService courseService;

    @Operation(summary = "获取课程详情")
    @GetMapping("/{courseId}")
    public RestBean<CourseVO> getCourse(
            @Parameter(description = "课程ID", required = true)
            @PathVariable Long courseId) {
        return courseService.getCourseByCourseId(courseId);
    }

    @Operation(summary = "创建课程")
    @PostMapping("/")
//    @ResponseStatus(HttpStatus.CREATED)
    public RestBean<CourseVO> createCourse(
            @RequestBody @Validated CourseDTO dto) {
        return courseService.addCourse(dto);
    }

    @Operation(summary = "更新课程信息")
    @PutMapping("/{courseId}")
    public RestBean<CourseVO> updateCourse(
            @RequestBody @Validated CourseDTO dto) {
        return courseService.updateCourse(dto);
    }

    @Operation(summary = "删除课程")
    @DeleteMapping("/{courseId}")
    public RestBean<CourseVO> deleteCourse(
            @PathVariable Long courseId) {
        return courseService.deleteCourseById(courseId);
    }

    @Operation(summary = "获取老师教授的所有课程")
    @GetMapping("/teacherId/{teacherId}")
    public RestBean<List<CourseVO>> getCourseByTeacherId(
            @Parameter(description = "教师ID", required = true)
            @PathVariable Long teacherId) {
        return courseService.getCourseByTeacherId(teacherId);
    }

    @Operation(summary = "分页查询课程")
    @GetMapping("/page")
    public RestBean<PageVO<CourseVO>> getCoursePage(
            @Validated CoursePageQueryDTO queryDTO) {
        return courseService.listByPage(queryDTO);
    }

}
